<template>
  <div class="container">
    <div class="row">
      <div class="col">
        <h1 class="text-center m-4">{{ msg }}</h1>
      </div>
    </div>
    <div class="row py-2">
      <div class="col d-grid gap-2">
        <button @click="linkWallet" class="btn btn-primary btn-block">连接钱包</button>
      </div>
    </div>
    <div class="row py-2">
      <div class="col d-grid gap-2">
        <button @click="getBalance" class="btn btn-primary">获取钱包余额</button>
      </div>
    </div>
    <div class="row py-2">
      <div class="col d-grid gap-2">
        <button @click="getAccount" class="btn btn-primary">获取账号信息</button>
      </div>
    </div>
    <div class="row py-2">
      <div class="col d-grid gap-2">
        <button @click="transaction" class="btn btn-primary">TRX转账交易</button>
      </div>
    </div>
    <div class="row py-2">
      <div class="col d-grid gap-2">
        <button @click="transactionToken" class="btn btn-primary">TRX-ERC20转账交易</button>
      </div>
    </div>

    <div class="row py-2">
      <div class="col d-grid gap-2">
        <button @click="transactionContract" class="btn btn-primary">合约方法调用</button>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
export default {
  name: 'Test',
  data() {
    return {
      msg: 'twdapp',
      /** @returns {any}  */
      tronWeb: {},
      walletAddress: {},
    }
  },
  mounted() {
    // if ((window as any).tronWeb) {
    // this.tronWeb = window.tronWeb
    //   console.log((this.tronWeb as any).defaultAddress)
    // this.walletAddress = this.tronWeb.defaultAddress.base58
    // }
  },
  methods: {
    linkWallet() {
      // if (this.tronWeb) {
      //   this.tronWeb = this.tronWeb
      //   console.log(this.tronWeb)
      // this.walletAddress = this.tronWeb.defaultAddress.base58
      //   console.log(this.walletAddress)
      // }
    },
    //查询钱包余额
    async getBalance() {
      // 当前连接的钱包地址获取 window.tronWeb.defaultAddress.base58
      // var balance = await this.tronWeb.trx.getBalance(this.walletAddress)
      // console.log('balance=', balance)
    },
    //查询钱包余额
    async getAccount() {
      //当前连接的钱包地址获取 window.tronWeb.defaultAddress.base58
      // var balance = await this.tronWeb.trx.getAccount(this.walletAddress)
      // console.log('balance=', balance)
    },
    //trx转账交易
    async transaction() {
      // var tx = await this.tronWeb.transactionBuilder.sendTrx(
      //   'TN9RRaXkCFtTXRso2GdTZxSxxwufzxLQPP',
      //   10 * Math.pow(10, 6),
      //   this.walletAddress
      // )
      // var signedTx = await this.tronWeb.trx.sign(tx)
      // var broastTx = await this.tronWeb.trx.sendRawTransaction(signedTx)
      // console.log(broastTx)
    },
    //trx-token转账交易
    async transactionToken() {
      //转账方式1
      // let contract = await this.tronWeb
      //   .contract()
      //   .at('TURwoLuFy7maq1Vea7wVwRNz3HgmcAsJzb')
      // let result = await contract
      //   .transfer(
      //     'TN9RRaXkCFtTXRso2GdTZxSxxwufzxLQPP',
      //     this.tronWeb.toHex(55 * Math.pow(10, 18))
      //   )
      //   .send({
      //     feeLimit: 10000000,
      //   })
      //   .then((output) => {
      //     console.log('- Output:', output, '\n')
      //   })
      // console.log('result: ', result)
      //转账方式2
      /*const parameter = [{type:'address',value:'TN9RRaXkCFtTXRso2GdTZxSxxwufzxLQPP'},{type:'uint256',value:this.tronWeb.toHex(25 * Math.pow(10,18))}]
      var tx  = await this.tronWeb.transactionBuilder.triggerSmartContract("TURwoLuFy7maq1Vea7wVwRNz3HgmcAsJzb", "transfer(address,uint256)", {},parameter,this.walletAddress);
      var signedTx = await this.tronWeb.trx.sign(tx.transaction);
      var broastTx = await this.tronWeb.trx.sendRawTransaction(signedTx);
      console.log(broastTx)*/
      /*let contract = await this.tronWeb.contract().at("TURwoLuFy7maq1Vea7wVwRNz3HgmcAsJzb");
     let result1 = await contract.decimals().call();
     console.log('result: ', result1);*/
    },
    //调用合约中的方法
    async transactionContract() {
      //调用方式1
      /*let contract = await this.tronWeb.contract().at("TSbJGFA8UyYGTuXBRbYB2GJeh2CY1X5F4d");
      console.log("contract=",contract)
      let result = await contract.registrationExt(
        "TN9RRaXkCFtTXRso2GdTZxSxxwufzxLQPP"
      ).send({
        callValue: this.tronWeb.toHex(25 * Math.pow(10,6)),//发送TRX余额
        feeLimit: 10000000
      }).then(output => {console.log('- Output:', output, '\n');});
      console.log('result: ', result)*/
      //调用方式2
      // const parameter = [
      //   { type: 'address', value: 'TN9RRaXkCFtTXRso2GdTZxSxxwufzxLQPP' },
      // ]
      // var tx = await this.tronWeb.transactionBuilder.triggerSmartContract(
      //   'TSbJGFA8UyYGTuXBRbYB2GJeh2CY1X5F4d',
      //   'registrationExt(address)',
      //   {},
      //   parameter,
      //   this.walletAddress
      // )
      // var signedTx = await this.tronWeb.trx.sign(tx.transaction)
      // var broastTx = await this.tronWeb.trx.sendRawTransaction(signedTx)
      // console.log(broastTx)
    },
  },
}
</script>

<style lang="scss" scoped>
@import '../../assets/scss/variables.scss';
h1,
h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
